/* $Id: Diet3DApplet.java.in,v 1.4 2008-10-24 05:44:04 rzr Exp $ */
#include "HackJavaCpp.java.in"

import java.awt.*;
import java.applet.Applet;

/**
 * @author www.Philippe.COVAL.free.fr
 * Copyright and License : http://rzr.online.fr/license.htm
 * $Id: Diet3DApplet.java.in,v 1.4 2008-10-24 05:44:04 rzr Exp $
 **/
public final class Diet3DApplet
  extends Applet
  implements Runnable
{
  public static Diet3D mRender = null;
  int h_ = 200;
  int w_ = h_;
  Image  offi_ = null;
  Thread thread_ = null;
  final long mspf = 40;// 1 / fps

  public Diet3DApplet()
  {
    //w_ = getSize().width;
    //h_ = getHeight(); //.height;
    //trace("Applet size="+w_+"*"+h_);
    mRender  = new Diet3D(w_,h_);
    if ( offi_ == null ) offi_ = createImage( w_, h_ );
  }

  #ifdef FULL
  #ifdef WANT_TRACE
  public String trace(Object o)
  {
    //if ( true) return ""; //
    String s= (o!=null) ? o.toString() : "null";
    MACRO_println(s);
    //showStatus(s);
    return s;
  }
  #endif
  #endif

  public void init()
  {
    if ( offi_ == null ) offi_ = createImage( w_, h_ );
    //try { wait(); } catch (Exception e) {  } //!!sync
    super.init();
    //trace("#{ init");
  }

  public void start()
  {
    if ( thread_ == null ) thread_ = new Thread(this);
    thread_.start();
  }

  public void stop() {
    thread_.stop();
    super.stop();
  }

  public void destroy() {
    if ( ( thread_ != null ) && (  thread_.isAlive()) ) {
      thread_.stop();
      thread_ = null ;
    }
  }

  public void paint(Graphics g)
  {
    if ( g != null) { update(g); } //WORKAROUND: possible rim bug?
  }

  // cant explain why but this shall be update(Graphics)
  // and nothing else
  public void update(Graphics g)
  {
    //trace("#{update");
    if ( offi_ == null ) offi_ = createImage( w_, h_ );// application
    //offi_.getGraphics().fillRect( 0 , 0 , w_ , h_ );
    mRender.paint( offi_.getGraphics() );
    g.drawImage( offi_ ,0,0 , this);
  }

  public void run()
  {
    long time = 0;
    //trace("#{run");
    //if ( off_ == null ) init();
    while( thread_ != null ) {
      time = System.currentTimeMillis();
      repaint();
      mRender.update();
      //try { wait(); } catch (Exception e) {  } //!!sync
      time = ( System.currentTimeMillis() - time );
      time = mspf - time ;
      if ( time < 0 ) time = 0;
      //time = 100;
      //trace(""+time);
      try {
        Thread.currentThread();
        Thread.sleep( time ) ; }
      catch ( InterruptedException  e ) {
        //trace("Exeption");
      }
    }
  }

  //-------------------------------------------------------------------------
  //
  // Event Handling - Java 1.0
  //

  public boolean handleEvent(Event evt)
  {
    switch ( evt.id ) {
    case Event.KEY_PRESS  : {
      try { handleKeyPressed (  evt.key); }
      catch ( Exception e) {  }
      break ;
    }
    case Event.KEY_ACTION  : {
      try { handleKeyPressed (  evt.key); }
      catch ( Exception e) {  }
      break ;

    }
    }
    return super.handleEvent(evt);
  }

  public void handleKeyPressed(int i) throws Exception
  {

    switch (i) {
    default :
      mRender.toggleMode();
    }
  }

  public boolean mouseDrag(Event e, int x , int y)
  {
    return true;
  }

  public boolean mouseDown(Event e, int x , int y)
  {
    mRender.toggleMode();
    return true;
  }


  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public static void main( String[] arg)
  {
    Diet3DApplet o = new Diet3DApplet();
    Frame f = new Frame();

    f.resize( o.w_ , o.h_ );
    f.add(o);
    o.init();
    o.start();
    o.validate();
    o.repaint();
    f.show();
    //o.stop();
  }
}

//#eof "$Id:$"
